package com.shawn.admin.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.shawn.admin.interceptor.ThreadContext;
import com.shawn.admin.service.SystemService;
import com.shawn.commons.utils.RC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@RestController ///system/users/verification system
@RequestMapping("/system/users")
public class SystemController {



    @Autowired
    private SystemService systemService;

    /**
     * 获取验证码
     */
    @GetMapping("/verification")
    public void getCode(String uuid, HttpServletResponse response) throws IOException {
        systemService.getCode(uuid,response);
    }

    @PostMapping("/login")
    public ResponseEntity loginCheck(@RequestBody Map<String,String> map){
        //获取请求体参数
        String username = map.get("username");
        String password = map.get("password");
        String code = map.get("verificationCode");
        String uuid = map.get("uuid");
        //返回用户id及用户名的token
        return systemService.loginCheck(username,password,code,uuid);
    }

    @PostMapping("/profile")
    public ResponseEntity getUserInfo(){
        Long id = ThreadContext.getId();
        return systemService.getUserInfo(id);
    }

    @PostMapping("/logout")
    public ResponseEntity logout(){
        //销毁token
        systemService.logout();
        return ResponseEntity.ok("退出成功!");
    }
}
